<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>销售订单相关的出库单记录</title>
    <script type="text/javascript">
      var deliveryId;
      Ext.onReady(function(){
        //成本总值
        var costAmountLabel = new Ext.form.Label({
          name: 'cost_amount_label'
        })
        costAmountLabel.setText("成本总值:0");

        //成本总值(含税)
        var taxAmountLabel = new Ext.form.Label({
          name: 'tax_amount_label'
        })
        costAmountLabel.setText("成本总值:0");
        taxAmountLabel.setText("成本总值(含税):0");
        
        var deliveryForSaleDS = new Ext.data.Store({
          baseParams:{id:'<%=params[:id]%>'},
          proxy: new Ext.data.HttpProxy({
            url: '/delivery_order/getDeliveryOrdersBySaleId'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'sale_order_number',
              mapping:'sale_order.order_number'
            },{
              name: 'order_number'
            }, {
              name: 'action_date'
            }, {
              name: 'cost_amount',
              type:'float'
            }, {
              name: 'tax_amount',
              type:'float'
            }, {
              name: 'description'
            }, {
              name: 'employee_name',
              mapping:'employee.name'
            },{
              name: 'department_name',
              mapping:'department.name'
            }, {
              name: 'warehouse_name',
              mapping:'warehouse.name'
            }, {
              name: 'customer_name',
              mapping:'customer.name'
            }, {
              name: 'auditing_status'
            }, {
              name: 'deal_with_status'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });

        //生成出库Grid的CM
        var deliveryForSaleCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '销售订单单号',
              dataIndex: 'sale_order_number',
              width: 180
            },{
              header: '出库单号',
              dataIndex: 'order_number',
              width: 130
            },{
              header: '客户',
              dataIndex: 'customer_name',
              width: 160
            },{
              header: '调出仓库',
              dataIndex: 'warehouse_name',
              width: 80
            },{
              header: '出库日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 75
            },{
              header: '成本总额',
              dataIndex: 'cost_amount',
              align:'right',
              width: 85
            },{
              header: '成本总额(含税)',
              dataIndex: 'tax_amount',
              align:'right',
              width: 110
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 60
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 70
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              renderer: auditingStatusRender,
              width: 60
            },{
              header: '订单状态',
              dataIndex: 'deal_with_status',
              renderer: dealWithStatusRender,
              width: 60
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            }
          ]});

        //生成要订单的产品的Grid
        var deliveryForSaleGrid = new Ext.grid.GridPanel({
          title:'出库单列表',
          store: deliveryForSaleDS,
          cm: deliveryForSaleCM,
          loadMask: true,
          width: 1080,
          height: 270,
          frame: true,
          tbar: [{
              text: '导出为Excel文件',
              iconCls: 'xls',
              handler : function(){
                var queryStr = "/sale_order/exportDeliveryOrderToXLS?id=<%=params[:id]%>";
                window.open(queryStr,'_blank','width=1,height=1,toolbar=no,menubar=no,location=no');
              }
            }],
          bbar: ['->',taxAmountLabel,'-',costAmountLabel]
        });
        
        deliveryForSaleDS.load({callback: function(record, options, success){
            if (financialManagement === '是'){
              costAmountLabel.setText("成本总值:"+Ext.util.Format.number(deliveryForSaleDS.reader.jsonData.sum_cost_amount,'0,000.00'));
              taxAmountLabel.setText("成本总值(含税):"+Ext.util.Format.number(deliveryForSaleDS.reader.jsonData.sum_tax_amount,'0,000.00'));
            }
          }
        });

        deliveryForSaleGrid.on('rowdblclick', function(gridPanel, rowIndex, e){
          deliveryId = deliveryForSaleDS.getAt(rowIndex).get('id');
          var saleOrderNumber = deliveryForSaleDS.getAt(rowIndex).get('order_number');
          //deliveryForSaleDS.reload();
          deliveryOrderDetailDS.removeAll();
          deliveryOrderDetailDS.baseParams.delivery_order_id = deliveryId;
          deliveryOrderDetailDS.load();
          deliveryOrderDetailGrid.setTitle("出库单："+saleOrderNumber+"，的详细信息");
        });

        //--------------------------出库单详细，双击后可显示数据----------------
        var deliveryOrderDetailDS = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: '/delivery_order/getDeliveryOrderDetailsById'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'product_name',
              mapping:'product.name'
            },{
              name: 'product_number',
              mapping:'product.number'
            },{
              name: 'product_spec',
              mapping:'product.spec'
            },{
              name: 'quantity',
              type: 'int'
            }, {
              name: 'unit_price',
              type: 'float'
            }, {
              name: 'discount',
              type: 'float'
            }, {
              name: 'amount',
              type: 'float'
            }, {
              name: 'cost_price',
              type: 'float'
            }, {
              name: 'cost_amount',
              type: 'float'
            }, {
              name: 'tax_price',
              type: 'float'
            }, {
              name: 'tax_amount',
              type: 'float'
            }, {
              name: 'is_suit',
              mapping:'product.is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            }, {
              name: 'description'
            }])
        });

        //生成销售订单详细表的Grid的CM
        var deliveryOrderDetailCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 230
            },{
              header: '编号',
              dataIndex: 'product_number',
              width: 70
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 80
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 80
            },{
              header: '销售单价',
              dataIndex: 'unit_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 70
            },{
              header: '折扣',
              dataIndex: 'discount',
              renderer: Ext.util.Format.numberRenderer('0,000.000000'),
              width: 70
            },{
              header: '销售总额',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 80
            },{
              header: '成本单价',
              dataIndex: 'cost_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.0000000000'),
              width: 110
            },{
              header: '成本总额',
              dataIndex: 'cost_amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 90
            },{
              header: '成本单价(含税)',
              dataIndex: 'tax_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 100
            },{
              header: '成本总额(含税)',
              dataIndex: 'tax_amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 110
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 170
            }
          ]});
        //生成要订单的产品的Grid
        var deliveryOrderDetailGrid = new Ext.grid.GridPanel({
          title:'出库单详细列表',
          store: deliveryOrderDetailDS,
          cm: deliveryOrderDetailCM,
          loadMask: true,
          width: 1080,
          height: 295,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });
        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: 1090,
          height: 590,
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[deliveryForSaleGrid,deliveryOrderDetailGrid]
        });
        workbenchPanel.render("delivery_for_sale_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="delivery_for_sale_workbench_panel"></div>
  </body>
</html>